package com.wenqiner.requesttool.core.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author rhf
 * @date 2023-04-03
 **/
public class CustomerThreadFactory implements ThreadFactory {

    private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
    private final AtomicInteger threadNumber;
    private final String namePrefix;

    public CustomerThreadFactory(String namePrefix) {
        this.threadNumber = new AtomicInteger(1);
        this.namePrefix = namePrefix + "-" + POOL_NUMBER.getAndIncrement() + "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        Thread t = new Thread(tg, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
        t.setDaemon(false);
        t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
}
